package com.xiyou.weblog.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiyou.weblog.admin.model.vo.blogsettings.FindBlogSettingsRspVO;
import com.xiyou.weblog.admin.model.vo.blogsettings.UpdateBlogSettingsReqVO;
import com.xiyou.weblog.admin.service.AdminBlogSettingsService;
import com.xiyou.weblog.common.domain.dos.BlogSettingsDo;
import com.xiyou.weblog.common.domain.mapper.BlogSettingsMapper;
import com.xiyou.weblog.common.utils.Response;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class AdminBlogSettingsServiceImpl extends ServiceImpl<BlogSettingsMapper,BlogSettingsDo> implements AdminBlogSettingsService {

    @Resource
    private BlogSettingsMapper blogSettingsMapper;
    /**
     * 更新博客设置
     */
    @Override
    public Response updateBlogSettings(UpdateBlogSettingsReqVO updateBlogSettingsReqVO) {
        // VO 转 DO
        BlogSettingsDo blogSettingsDo = BlogSettingsDo.builder()
                .id(1L)
                .logo(updateBlogSettingsReqVO.getLogo())
                .name(updateBlogSettingsReqVO.getName())
                .author(updateBlogSettingsReqVO.getAuthor())
                .introduction(updateBlogSettingsReqVO.getIntroduction())
                .avatar(updateBlogSettingsReqVO.getAvatar())
                .githubHomepage(updateBlogSettingsReqVO.getGithubHomepage())
                .giteeHomepage(updateBlogSettingsReqVO.getGiteeHomepage())
                .csdnHomepage(updateBlogSettingsReqVO.getCsdnHomepage())
                .zhihuHomepage(updateBlogSettingsReqVO.getZhihuHomepage())
                .build();

/*        BlogSettingsDo blogSettingsDo = BlogSettingConvert.INSTANCE.convertVO2DO(updateBlogSettingsReqVO);
        blogSettingsDo.setId(1L);
        用不了*/

        // 保存或更新（当数据库中存在 ID 为 1 的记录时，则执行更新操作，否则执行插入操作）
        saveOrUpdate(blogSettingsDo);
        return Response.success();

        /*
        AdminBlogSettingsServiceImpl 继承自了 Mybatis Plus
        内部封装的 ServiceImpl 业务实现类，该类提供了很多封装好的业务方法，
        可以直接拿来使用，如 saveOrUpdate() 方法，顾名思义，
        此方法是个保存或更新的方法，当需要操作的记录已存在表中时，
        则执行更新操作，否则执行 insert 操作
         */
    }

    @Override
    public Response findDetail() {
        // 获取信息
        BlogSettingsDo blogSettingsDo = blogSettingsMapper.selectById(1L);

        // DO 转 VO
        FindBlogSettingsRspVO findBlogSettingsRspVO = FindBlogSettingsRspVO.builder()
                .logo(blogSettingsDo.getLogo())
                .name(blogSettingsDo.getName())
                .author(blogSettingsDo.getAuthor())
                .introduction(blogSettingsDo.getIntroduction())
                .avatar(blogSettingsDo.getAvatar())
                .githubHomepage(blogSettingsDo.getGithubHomepage())
                .giteeHomepage(blogSettingsDo.getGiteeHomepage())
                .csdnHomepage(blogSettingsDo.getCsdnHomepage())
                .zhihuHomepage(blogSettingsDo.getZhihuHomepage())
                .build();

        return Response.success(findBlogSettingsRspVO);
    }
}
